﻿using DG.Tweening;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UIBattlePlayer : UIObject
{
    public GameObject buttonRoot;
    //public Text[] textDices;
    public Text textCardType;
    public Text textVote;
    public GameObject diceRoot;
    public GameObject cup;
    public Text textPlayer;
    //public UIBattlePlayerHand playerHand;
    //public UIBattlePlayerChallenge playerChallenge;
    public UIBattlePlayerChat playerChat;
    //public UIBattlePlayerDice playerDice;


    public GameObject diverRoot;
    public GameObject voteRoot;
    protected override void Awake()
    {
        base.Awake();
    }

    protected override void OnDestroy()
    {
        base.OnDestroy();
        Unbind();
    }


    private DataBattlePlayer _dataBattlePlayer;
    public void Bind(DataBattlePlayer data)
    {
        //playerHand?.Bind(data);
        //playerChallenge?.Bind(data);
        playerChat?.Bind(data);
        _dataBattlePlayer = data;
        _dataBattlePlayer.onCardTypeChange += OnCardTypeChange;
        _dataBattlePlayer.OnPlayerExitBattle += OnPlayerExitBattle;
        _dataBattlePlayer.onIsDriver += OnIsDriver;
        _dataBattlePlayer.onSupportTypeChange += onSupportTypeChange;

        if (data.GetUserId() == DataPlayer.Instance.userId)
        {
            textPlayer.color = Color.green;
            textPlayer.text = "本人:" + data.GetUserName();
        }
        else
        {
            textPlayer.text = data.GetUserName();
        }

        OnCardTypeChange(_dataBattlePlayer.GetCardType());
        Refresh();
    }

    public void Unbind()
    {
        //playerHand?.Unbind();
        //playerChallenge?.Unbind();
        playerChat?.Unbind();
        _dataBattlePlayer.onCardTypeChange -= OnCardTypeChange;
        _dataBattlePlayer.OnPlayerExitBattle -= OnPlayerExitBattle;
        _dataBattlePlayer.onIsDriver -= OnIsDriver;
        _dataBattlePlayer.onSupportTypeChange -= onSupportTypeChange;
        _dataBattlePlayer = null;
    }

    private void Refresh()
    {
        //是否轮到我喊骰子
        this.RefreshDirver(_dataBattlePlayer.IsDriver());
        this.RefreshVote();
        //不能操作其他人按钮
        this.RefreshButton();
    }

    private void RefreshDirver(bool isDriver)
    {
        this.diverRoot.SetActive(isDriver);
    }

    private void RefreshVote()
    {
        var supportType = _dataBattlePlayer.GetSupportType();
        this.voteRoot.SetActive(supportType != SupportType.Invalid);

        switch (supportType)
        {
            case SupportType.Invalid:
                break;
            case SupportType.Support:
                textVote.text = "✓";
                break;
            case SupportType.UnSupport:
                textVote.text = "x";
                break;
            case SupportType.SupportSecret:
                textVote.text = this._dataBattlePlayer.GetUserId() == DataPlayer.Instance.userId ? "Yes" : "?";
                break;
            case SupportType.UnSupportSecret:
                textVote.text = this._dataBattlePlayer.GetUserId() == DataPlayer.Instance.userId ? "No" : "?";
                break;
            default:
                Debug.LogError("unhandle type:" + supportType);
                break;
        }
    }

    private bool _isRefreshButton = false;
    private void RefreshButton() {
        if (_isRefreshButton) return;
        _isRefreshButton = true;
        var isShow = this._dataBattlePlayer.GetUserId() == DataPlayer.Instance.userId;
        buttonRoot.SetActive(isShow);
    }

    public void OnButtonYesClick(Button button)
    {
        if(this._dataBattlePlayer.GetUserId() != DataPlayer.Instance.userId)
        {
            UITips.Instance.Show("非法操作");
            return;
        }
        
        var req = new vote();
        req.support_type = (int)SupportType.Support;
        WebSocketClient.Instance.SendSerialize(button, req);
    }

    public void OnButtonNoClick(Button button)
    {
        if (this._dataBattlePlayer.GetUserId() != DataPlayer.Instance.userId)
        {
            UITips.Instance.Show("非法操作");
            return;
        }
        var req = new vote();
        req.support_type = (int)SupportType.UnSupport;
        WebSocketClient.Instance.SendSerialize(button, req);
    }

    public void OnButtonYesSecretClick(Button button)
    {
        if (this._dataBattlePlayer.GetUserId() != DataPlayer.Instance.userId)
        {
            UITips.Instance.Show("非法操作");
            return;
        }

        var req = new vote();
        req.support_type = (int)SupportType.SupportSecret;
        WebSocketClient.Instance.SendSerialize(button, req);
    }

    public void OnButtonNoSecretClick(Button button)
    {
        if (this._dataBattlePlayer.GetUserId() != DataPlayer.Instance.userId)
        {
            UITips.Instance.Show("非法操作");
            return;
        }
        var req = new vote();
        req.support_type = (int)SupportType.UnSupportSecret;
        WebSocketClient.Instance.SendSerialize(button, req);
    }

    public void OnButtonCancelClick(Button button)
    {
        if (this._dataBattlePlayer.GetUserId() != DataPlayer.Instance.userId)
        {
            UITips.Instance.Show("非法操作");
            return;
        }

        //清除场上所有投票
        WebSocketClient.Instance.SendMsgName(button, nameof(voteclear));
    }

    private void OnCardTypeChange(CardType cardType)
    {
        ShowCardType(cardType);
    }

    private void OnPlayerExitBattle(string reason)
    {
        textPlayer.text = _dataBattlePlayer.GetUserName() + ":" + reason;
    }

    private void OnIsDriver(bool isDirver)
    {
        this.RefreshDirver(isDirver);
    }

    private void onSupportTypeChange(SupportType supportType)
    {
        this.RefreshVote();
    }

    private void ShowCardType(CardType cardType)
    {
        if (_dataBattlePlayer.GetUserId() == DataPlayer.Instance.userId)
            textCardType.text = CardTypeHelper.GetCardTypeNameEng(cardType) + "\n" + CardTypeHelper.GetCardTypeNameCHS(cardType);
        else
        {
            var player = BattleCtr.Instance.dataBattleMgr.GetDataPlayer(DataPlayer.Instance.userId);
            textCardType.text = CardTypeHelper.GetVisibleName(player.GetCardType(), cardType);
        }
    }

    private bool IsShowDice()
    {
        return diceRoot.activeSelf;
    }

    public void SetShowIndex(int showIndex)
    {
        this.transform.SetParent(UIBattle.Instance.playerRoots[showIndex].transform, false);
        //var parent = playerHand.transform.parent.GetComponent<RectTransform>();
        //var oldPosition = parent.anchoredPosition;
        //if (showIndex > 4)
        //{
        //    oldPosition.x = Mathf.Abs(oldPosition.x) * -1;
        //    parent.anchoredPosition = oldPosition;
        //}
    }

}
